<?php
set_time_limit(0);
error_reporting(E_ALL);
require_once './Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'ID')
            ->setCellValue('B1', 'uid')
            ->setCellValue('C1', 'ip')
            ->setCellValue('D1', 'tag')
            ->setCellValue('E1', '客户端')
            ->setCellValue('F1', '时间')
;
include 'c.php';
$index=2;

$day=date("Y-m-d");
$tag='all';
if (isset($_GET['tag'])){
    $tag=$_GET['tag'];
}


$mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
if (!$mysqli->set_charset("utf8")) {printf("Error loading character set utf8: %s\n", $mysqli->error);}

$thetag=$tag;
if ($thetag != 'all'){
	$sql="SELECT id,uid,ip,tag,detail,createtime FROM analyse where tag='".$thetag."' order by id desc ";
}else{
	$sql="SELECT id,uid,ip,tag,detail,createtime FROM analyse order by id desc ";
}

$query=$mysqli->query($sql);
 
while ($list=$query->fetch_assoc()){
	$detail=json_decode($list['detail'], true);
	
	$os=$detail['os'];
	$browser=$detail['b'];
	if ('Other' == $detail['os']){
		$os='未知';
		$browser='';
	}
        $objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A'.$index, $list['id'])
        ->setCellValue('B'.$index, $list['uid']." ")
        ->setCellValue('C'.$index, $list['ip'])
        ->setCellValue('D'.$index, $list['tag'])
        ->setCellValue('E'.$index, $os.$browser)
        ->setCellValue('F'.$index, $list['createtime'])
        ;
        $index++;
}
     
 
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('用户数据');

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="用户数据.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
